<--- %%NOBANNER%% --> MacroVarChk.sas
 BackForward

/*-------------------<---Start of Description-->---------------------\
| Check the existence of a global macro variable;                    |
|---------------------<---End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<---Start of Files or Arguments Needed-->---------------|
| Parameters: (!=required)                                           |
|    NAME: name of the macro variable;                               |
|    return: if you want to return value (to assign to another var); |
|          F - no return value;                                      |
|          T - return a value;                                       |
|------------------<---End of Arguments Needed-->--------------------|
|--------------------------------------------------------------------|
|------------------<---Start of Files Created-->---------------------|
| Example (check if &a exists                                        |
|     %macrovarchk(a)                                                |
|     %MacroVarChk(%nrstr(&hascolor));                               |
|     %let rc=%MacroVarChk(%nrstr(&hascolor), return=T);             |
|     RESULT in the log window: hascolor Exist: No;                  |
|     rc=1                                                           |
| Usage: %MacroVarChk(name,return=F);                                |
\-------------------------------------------------------------------*/
%MACRO MacroVarChk(name); 
/*--------------------------------------------\
| Author:  Duo Zhou;                          |
| Created: 9-28-2001 8:38pm;                  |
| Purpose: Check the existence of a global    |
|          macro variable;                    |
\--------------------------------------------*/
%local rc;
%let Name=%sysfunc(dequote(&&Name));
%if (%index(&&Name,%str(&))) %then %let Name=%sysfunc(compress(&&Name, %str(&)));
%if (%nrquote(&&&name)=%nrstr(&)&name) %then %do;
   %let yesno=NO; 
   %let rc=0;
%end;
%else %do;
   %let yesno=YES; 
   %let rc=1;
%end;&rc;
%put --> Note: %nrstr(&)&name Exist: &yesno.. ;
%MEND MacroVarChk;
%let test=this is a test;
%put %macrovarchk(test);